package org.example.teaView;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class SeeAllTopicView extends JFrame implements Runnable{
    HandleSeeAllTopics handleSeeAllTopics;
    JButton back,seeAll,search;
    JTextArea showResult;
    JScrollPane js;
    JTextField input_ID,hintMess;


    SeeAllTopicView(){
        init();
        seeAllTopicsListener();

    }
    private void init() {
        this.setSize(800,800);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);
        this.setTitle("查看所有题目页面-3220608045吉治霖软件2202");

        //输入ID查询
        Font font = new Font(null,1,16);
        JLabel IDText = new JLabel("题目序号：");
        IDText.setFont(font);
        IDText.setBounds(50,35,110,22);
        this.getContentPane().add(IDText);

        input_ID = new JTextField();
        input_ID.setBounds(150,35,600,22);
        this.getContentPane().add(input_ID);
        //显示框
        showResult = new JTextArea();
        showResult.setLineWrap(true);
        showResult.setEditable(false);
        js = new JScrollPane(showResult);
        js.setBounds(150,75,500,500);
        this.getContentPane().add(js);
        //查看所有按钮
        seeAll = new JButton("查看所有");
        seeAll.setBounds(100,600,120,30);
        this.getContentPane().add(seeAll);
        //搜索按钮
        search = new JButton("搜索");
        search.setBounds(350,600,80,30);
        this.getContentPane().add(search);
        //返回按钮
        back = new JButton("返回");
        back.setBounds(600,600,80,30);
        this.getContentPane().add(back);
        //提示框
        hintMess = new JTextField();
        hintMess.setEditable(false);
        hintMess.setBounds(50,700,700,30);
        this.getContentPane().add(hintMess);
    }

    private void seeAllTopicsListener() {
        handleSeeAllTopics = new HandleSeeAllTopics();
        handleSeeAllTopics.setSeeAllTopicView(this);
        seeAll.addActionListener(handleSeeAllTopics);
        search.addActionListener(handleSeeAllTopics);
        back.addActionListener(handleSeeAllTopics);
    }

    @Override
    public void run() {
        String s=null;
        while(true) {
            try{
                s=handleSeeAllTopics.in.readUTF();
                if (s.equals(null)){
                    break;
                }
                System.out.println(s);
                if (s.startsWith("搜索到题目ID")){
                    showResult.setText(s.substring(8)+"\n");
                    showResult.append("------------------------------------------------------------------------------------\n");
                } else if (s.startsWith("题目的总数为")) {
                    showResult.setText("");
                    hintMess.setText(s);
                } else if (s.startsWith("显示第")) {
                    showResult.append(s.substring(2)+"\n");
                } else if (s.startsWith("最后显示第")) {
                    showResult.append(s.substring(4)+"\n");
                    showResult.append("------------------------------------------------------------------------------------\n");
                }else showResult.setText(s+"\n");
            }
            catch(IOException e) {
                break;
            }
        }
    }
}
